part of '../repository.dart';

const _kPurchasedSubscriptionTransactionExpirationTimeKey = 'expiration_time';
const _kPurchasedSubscriptionTransactionOfferTypeKey = 'offer_type';

const _kPurchasedSubscriptionTransactionTypeValue = 'purchased_subscription_transaction';

class _PurchasedSubscriptionTransactionDto extends Equatable
    implements _PurchasedItemTransactionDto {
  const _PurchasedSubscriptionTransactionDto._internal({
    required this.googlePlayUserId,
    required this.purchaseToken,
    required this.purchaseItemSku,
    required this.test,
    required this.timeDto,
    required this.orderId,
    required this.expirationTimeDto,
    required this.offerTypeDto,
    required this.revocationInfoDto,
  });

  @override
  final String orderId;

  @override
  final String googlePlayUserId;

  @override
  final String purchaseToken;

  @override
  final String purchaseItemSku;

  @override
  final bool test;

  @override
  final _TimeDto timeDto;

  @override
  final _TransactionRevocationInfoDto? revocationInfoDto;

  final _TimeDto expirationTimeDto;
  final _PurchasedSubscriptionTransactionOfferTypeDto offerTypeDto;

  @override
  String get type => _kPurchasedSubscriptionTransactionTypeValue;

  @override
  List<Object?> get props {
    return [
      orderId,
      googlePlayUserId,
      purchaseToken,
      purchaseItemSku,
      test,
      timeDto,
      revocationInfoDto,
      expirationTimeDto,
      offerTypeDto,
    ];
  }

  factory _PurchasedSubscriptionTransactionDto.fromJson(Map<String, dynamic> json) {
    final expirationTimeString =
        json[_kPurchasedSubscriptionTransactionExpirationTimeKey] as String;
    final expirationTimeDto = _TimeDto.fromString(expirationTimeString);

    final offerTypeString = json[_kPurchasedSubscriptionTransactionOfferTypeKey] as String;
    final offerTypeDto = _PurchasedSubscriptionTransactionOfferTypeDto.fromString(offerTypeString);

    final timeString = json[_kPurchasedItemTransactionTimeKey] as String;
    final timeDto = _TimeDto.fromString(timeString);

    final revocationInfoData =
        json[_kPurchasedItemTransactionRevocationInfoKey] as Map<String, dynamic>?;

    _TransactionRevocationInfoDto? revocationInfoDto;

    if (revocationInfoData != null) {
      revocationInfoDto = _TransactionRevocationInfoDto.fromJson(revocationInfoData);
    }

    return _PurchasedSubscriptionTransactionDto._internal(
      orderId: json[_kPurchasedItemTransactionOrderIdKey],
      googlePlayUserId: json[_kPurchasedItemTransactionGooglePlayUserIdKey],
      purchaseToken: json[_kPurchasedItemTransactionPurchaseTokenKey],
      purchaseItemSku: json[_kPurchasedItemTransactionPurchaseItemSkuKey],
      test: json[_kPurchasedItemTransactionTestKey],
      timeDto: timeDto,
      expirationTimeDto: expirationTimeDto,
      offerTypeDto: offerTypeDto,
      revocationInfoDto: revocationInfoDto,
    );
  }

  factory _PurchasedSubscriptionTransactionDto.fromEntity(
      GooglePlayPurchasedSubscriptionTransaction transaction) {
    final expirationTimeDto = _TimeDto.fromEntity(transaction.expirationTime);

    final offerTypeDto =
        _PurchasedSubscriptionTransactionOfferTypeDto.fromEntity(transaction.offerType);

    final timeDto = _TimeDto.fromEntity(transaction.time);

    _TransactionRevocationInfoDto? revocationInfoDto;

    if (transaction.revocationInfo != null) {
      revocationInfoDto = _TransactionRevocationInfoDto.fromEntity(transaction.revocationInfo!);
    }

    return _PurchasedSubscriptionTransactionDto._internal(
      googlePlayUserId: transaction.googlePlayUserId,
      purchaseToken: transaction.purchaseToken,
      purchaseItemSku: transaction.purchaseItemSku,
      test: transaction.test,
      timeDto: timeDto,
      orderId: transaction.orderId,
      expirationTimeDto: expirationTimeDto,
      offerTypeDto: offerTypeDto,
      revocationInfoDto: revocationInfoDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kPurchasedItemTransactionTypeKey: type,
      _kPurchasedItemTransactionOrderIdKey: orderId,
      _kPurchasedItemTransactionGooglePlayUserIdKey: googlePlayUserId,
      _kPurchasedItemTransactionPurchaseTokenKey: purchaseToken,
      _kPurchasedItemTransactionPurchaseItemSkuKey: purchaseItemSku,
      _kPurchasedItemTransactionTestKey: test,
      _kPurchasedItemTransactionTimeKey: timeDto.toString(),
      _kPurchasedItemTransactionRevocationInfoKey: revocationInfoDto?.toJson(),
      _kPurchasedSubscriptionTransactionExpirationTimeKey: expirationTimeDto.toString(),
      _kPurchasedSubscriptionTransactionOfferTypeKey: offerTypeDto.toString(),
    };
  }

  @override
  GooglePlayPurchasedSubscriptionTransaction toEntity() {
    return GooglePlayPurchasedSubscriptionTransaction(
      orderId: orderId,
      googlePlayUserId: googlePlayUserId,
      purchaseToken: purchaseToken,
      purchaseItemSku: purchaseItemSku,
      test: test,
      time: timeDto.toEntity(),
      revocationInfo: revocationInfoDto?.toEntity(),
      expirationTime: expirationTimeDto.toEntity(),
      offerType: offerTypeDto.toEntity(),
    );
  }
}
